import { createApp, ref } from 'vue'
import HandDrawnToast from '@/components/HandDrawnToast.vue'

// 全局提示实例
let toastInstance: any = null

// 初始化手绘提示
const initHandDrawnToast = () => {
  if (toastInstance) return toastInstance

  // 创建容器
  const container = document.createElement('div')
  container.id = 'hand-drawn-toast-container'
  document.body.appendChild(container)

  // 创建Vue应用实例
  const app = createApp(HandDrawnToast)
  const vm = app.mount(container)

  toastInstance = vm
  return vm
}

// 确保实例存在
const getInstance = () => {
  if (!toastInstance) {
    initHandDrawnToast()
  }
  return toastInstance
}

// 导出的全局方法
export const handDrawnToast = {
  success: (message: string, title?: string, options?: any) => {
    const instance = getInstance()
    return instance.success(message, title, options)
  },

  error: (message: string, title?: string, options?: any) => {
    const instance = getInstance()
    return instance.error(message, title, options)
  },

  warning: (message: string, title?: string, options?: any) => {
    const instance = getInstance()
    return instance.warning(message, title, options)
  },

  info: (message: string, title?: string, options?: any) => {
    const instance = getInstance()
    return instance.info(message, title, options)
  },

  custom: (message: string, title?: string, emoji?: string, options?: any) => {
    const instance = getInstance()
    return instance.custom(message, title, emoji, options)
  },

  dismiss: (id: string) => {
    const instance = getInstance()
    return instance.dismiss(id)
  },

  clear: () => {
    const instance = getInstance()
    return instance.clear()
  }
}

// 兼容ElNotification的接口
export const HandDrawnNotification = {
  success: (options: { title?: string; message: string; duration?: number }) => {
    return handDrawnToast.success(options.message, options.title, { duration: options.duration })
  },

  error: (options: { title?: string; message: string; duration?: number }) => {
    return handDrawnToast.error(options.message, options.title, { duration: options.duration })
  },

  warning: (options: { title?: string; message: string; duration?: number }) => {
    return handDrawnToast.warning(options.message, options.title, { duration: options.duration })
  },

  info: (options: { title?: string; message: string; duration?: number }) => {
    return handDrawnToast.info(options.message, options.title, { duration: options.duration })
  },

  close: (id: string) => {
    return handDrawnToast.dismiss(id)
  },

  closeAll: () => {
    return handDrawnToast.clear()
  }
}

// 默认导出
export default handDrawnToast 